<template>
  <page-header-wrapper :title="false">
    <a-card title="意向订单">
      <div class="table-page-search-wrapper">
        <a-form-model
          layout="inline"
          :model="queryParam"
        >
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="商标名称">
                <a-input v-model="queryParam['filter[tm_name]']" placeholder="请输入商标名称"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="商标类别">
                <a-select v-model="queryParam['filter[category]']" placeholder="请选择商标类别">
                  <a-select-option v-for="(item, index) in categoryOpts" :key="index" :value="index">{{item}}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="国家">
                  <a-select v-model="queryParam['filter[region]']" placeholder="请选择国家">
                    <a-select-option v-for="(item, index) in regionOpts" :key="index" :value="index">
                      {{item}}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="注册号">
                  <a-input v-model="queryParam['filter[tm_reg_no]']" placeholder="请输入注册号"/>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="所属顾问">
                  <a-input v-model="queryParam['filter[username]']" placeholder="请输入所属顾问"/>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="进展状态">
                  <a-select v-model="queryParam['filter[status]']" placeholder="请选择进展状态">
                    <a-select-option v-for="(item, index) in statusOpts" :key="index" :value="index">
                      {{item}}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="发起时间">
                  <a-range-picker v-model="queryParam['created_at_start']" @change="onChange" />
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class='table-page-search-submitButtons' :style="advanced && { float: 'right', overflow: 'hidden' } || {}">
                <a-button type="primary" @click="$refs.table.refresh(true)">搜索</a-button>
                <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          ref="table"
          bordered
          :row-key="(record) => record.id"
          :columns="intentionColumns"
          :data="intentionData"
        >
          <div
            slot="filterDropdown"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-input
              v-ant-ref="c => (searchInput = c)"
              :placeholder="`搜索${column.title}`"
              :value="selectedKeys[0]"
              style="width: 188px; margin-bottom: 8px; display: block;"
              @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
              @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterCategory"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in categoryOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterRegion"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in regionOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterStatus"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in statusOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterRange"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-range-picker
              style="width: 228px; margin-bottom: 8px; display: block;"
              :value="createValue"
              @change="onChangeDate"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 110px; margin-right: 8px"
              @click="() => handleRange(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 110px" @click="() => rangeReset(clearFilters)">
              重置
            </a-button>
          </div>
          <template slot="category" slot-scope="text">
            <div v-for="(item, index) in categoryOpts" :key="index">
              <div v-if="text == index"><a-tag color="red">{{item}}</a-tag></div>
            </div>
          </template>
          <template slot="region" slot-scope="text">
            <div v-for="(item, index) in regionOpts" :key="index">
              <div v-if="text == index">{{item}}</div>
            </div>
          </template>
          <template slot="status" slot-scope="text,record">
            <a-button type="primary" size="small" @click="showVisible(record.id)">{{statusOpts[text]}}</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
    <a-modal
      centered
      title="进展状态编辑"
      v-model="visible"
      @ok="onSubmit"
    >
      <a-form-model
        ref="form"
        :model="statusForm"
      >
        <a-form-model-item label="进展状态">
          <a-radio-group v-model="statusForm.status">
            <a-radio v-for="(item,index) in intentOpts" :key="index" :value="index">{{item}}</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="交易备注">
          <a-textarea v-model="statusForm.notes" :rows="4" placeholder="注：如不能交易请填写原因"/>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import transactionColumns from '@/columns/transaction'
import { getEditIntent, getIntentList, postEditIntent } from '@/api/tm.shop'

export default {
  name: 'Intention',
  components: { STable, Ellipsis },
  data () {
    return {
      advanced: true,
      queryParam: {},
      visible: false,
      intentionColumns: transactionColumns.intention,
      form: {},
      rules: {
        status: [{ required: true, message: '要求修改，必须填写原因，方便专利师修改', trigger: 'blur' }]
      },
      categoryOpts: {},
      regionOpts: {},
      statusOpts: {},
      intentionData: parameters => {
        return getIntentList(Object.assign(parameters, this.queryParam)).then(res => {
          this.categoryOpts = res.result.category_opts
          this.regionOpts = res.result.tm_region_opts
          this.statusOpts = res.result.statusOpts
          return res.result
        })
      },
      statusForm: {},
      intentOpts: {},
      createValue: [],
      searchedColumn: ''
    }
  },
  methods: {
    resetForm () {
      this.queryParam = {}
      this.$refs.table.refresh(true)
    },
    showVisible (id) {
      const app = this
      app.statusForm = {}
      getEditIntent(id).then(res => {
        if (res.status === 0) {
          app.statusForm = res.result.intent
          app.intentOpts = res.result.status_items
          app.statusForm.id = id
          app.visible = true
        } else {
          app.$warning({
            title: res.msg
          })
        }
      })
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    onChange (date, dateString) {
      this.queryParam['filter[created_at_start]'] = dateString[0]
      this.queryParam['filter[created_at_end]'] = dateString[1]
    },
    onSubmit () {
      const app = this
      const data = {}
      data['intent[status]'] = app.statusForm.status
      data['intent[notes]'] = app.statusForm.notes
      postEditIntent(app.statusForm.id, data).then(res => {
        if (res.status === 0) {
          app.visible = false
          app.$refs.table.refresh()
        } else {
          app.$warning({
            title: res.msg
          })
        }
      })
    },
    handleSelect (selectedKeys, confirm, dataIndex) {
      confirm()
      this.searchText = selectedKeys[0]
      this.searchedColumn = dataIndex
      this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
    },
    handleSearch (selectedKeys, confirm, dataIndex) {
      confirm()
      this.searchText = selectedKeys[0]
      this.searchedColumn = dataIndex
      this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
    },
    handleReset (clearFilters, dataIndex) {
      clearFilters()
      delete this.queryParam['filter[' + dataIndex + ']']
    },
    onChangeDate (date, dateString) {
      this.createValue = date
      this.queryParam['filter[created_at_start]'] = dateString[0]
      this.queryParam['filter[created_at_end]'] = dateString[1]
    },
    handleRange (selectedKeys, confirm, dataIndex) {
      confirm()
      this.searchText = selectedKeys[0]
      this.searchedColumn = dataIndex
      this.$refs.table.refresh(true)
    },
    rangeReset (clearFilters) {
      clearFilters()
      this.createValue = []
      delete this.queryParam['filter[created_at_start]']
      delete this.queryParam['filter[created_at_end]']
      this.$refs.table.refresh(true)
    }
  }
}
</script>

<style scoped>

</style>
